/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package beans;

import com.icesoft.faces.component.ext.RowSelectorEvent;
import com.icesoft.faces.component.selectinputtext.SelectInputText;
import core.Shop;
import db.JDBC.JdbcDaoFactory;
import db.JDBC.core.ShopJdbcDAOCore;
import db.core.interfaces.ShopDAOCore;
import db.interfaces.ShopDAO;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.event.ActionEvent;
import javax.faces.event.ValueChangeEvent;
import javax.faces.model.SelectItem;

/**
 *
 * @author Anton
 */

public class WebShopListBean extends AutoCompleteBean
{

    List<WebShopBean> webshopList;
    private WebShopBean selectedWebshop;
    private int selectedRow;
    private boolean editMode;
    private boolean addMode;
    private String oldSearchString;
    private WebShopBean addShop; // used for storing shop to be added

    /*
     *  Sortable data tables
     */

    private final static String idColumnName = "id #";
    private final static String nameColumnName = "Shop Name";
    private final static String descriptionColumnName = "description";
    private final static String weblinkColumnName = "weblink";
    private final static String xmlsourceColumnName = "XML source link";

    private final static String dbIdColumnName = "id";
    private final static String dbNameColumnName = "name";
    private final static String dbDescriptionColumnName = "description";
    private final static String dbWeblinkColumnName = "weblink";
    private final static String dbXmlsourceColumnName = "xmlsource";

    private HashMap<String, String> lookupDbColumnName;

    boolean ascending;
    boolean oldAscending;
    private String sortColumnName;
    private String oldSortColumnName;

        public String getIdColumnName()
    {
        return idColumnName;
    }

    public String getNameColumnName()
    {
        return nameColumnName;
    }
    public String getDescriptionColumnName()
    {
        return descriptionColumnName;
    }

    public String getWeblinkColumnName()
    {
        return weblinkColumnName;
    }
        public String getXmlsourceColumnName()
    {
        return xmlsourceColumnName;
    }


   public void setSortColumnName(String name)
    {
        this.oldSortColumnName = this.sortColumnName;
        this.sortColumnName = name;
    }

    public String getSortColumnName()
    {
        return sortColumnName;
    }

    public void setAscending (boolean ascending)
    {
        oldAscending = this.ascending;
        this.ascending = ascending;
    }

    public boolean getAscending()
    {
        return ascending;
    }

        public List<WebShopBean> getWebshopList()
    {
        if(oldAscending != ascending || !sortColumnName.equals(oldSortColumnName))
        search(null);
        return webshopList;
    }

    public void setWebshopList(List<WebShopBean> webshopList)
    {
        this.webshopList = webshopList;
    }

    /*
     * Selection and editing
     *
     */

    public void setSelected(RowSelectorEvent ev)
    {
        this.selectedRow = ev.getRow();
        this.selectedWebshop = new WebShopBean(
                webshopList.get(ev.getRow())
                );
    }


    public WebShopBean getSelected()
    {
        return selectedWebshop;
    }

    public boolean getEditMode()
    {
        return editMode;
    }

    public void setEditMode(boolean editMode)
    {

        this.editMode = editMode;
    }

    public void enableEditMode(ActionEvent e)
    {
        if(selectedWebshop != null)
        {
        editMode = true;
        }
    }

    public void disableEditMode(ActionEvent e)
    {
        editMode = false;
    }
    public boolean getAddMode()
    {
        return addMode;
    }

    public void setAddMode(boolean addMode)
    {

        this.addMode = addMode;
    }

    public void enableAddMode(ActionEvent e)
    {
        addShop = new WebShopBean(new Shop());
        addMode = true;

    }

    public void disableAddMode(ActionEvent e)
    {
        addMode = false;
    }

        public void updateWebshop(ActionEvent e) 
    {
        JdbcDaoFactory.getInstance().getShopJdbcDAO().editShop(selectedWebshop.getShop());
        webshopList.get(selectedRow).setShop(selectedWebshop.getShop());
        editMode = false;

    }
        public void addWebShop(ActionEvent e)
        {
            JdbcDaoFactory.getInstance().getShopJdbcDAO().addShop(addShop.getShop());
            search(null);
            addMode = false;
        }

    public void deleteWebShop(ActionEvent e)
    {
        
        JdbcDaoFactory.getInstance().getShopJdbcDAO().deleteShop(selectedWebshop.getShop());
        webshopList.remove(selectedRow);
        editMode = false;
        
    }

     private void initDbColumns()
    {
        lookupDbColumnName = new HashMap<String, String>();
        lookupDbColumnName.put(idColumnName, dbIdColumnName);
        lookupDbColumnName.put(nameColumnName, dbNameColumnName);
        lookupDbColumnName.put(descriptionColumnName, dbDescriptionColumnName);
        lookupDbColumnName.put(weblinkColumnName, dbWeblinkColumnName);
        lookupDbColumnName.put(xmlsourceColumnName, dbXmlsourceColumnName);
    }

    @PostConstruct
    public void init()
    {
        initDbColumns();
        ascending = true;
        oldSearchString = searchString = "";
        oldSortColumnName = sortColumnName = idColumnName;
        search(null);
    }



    /*
     * Search
     *
     *
     */
public void search(ActionEvent e)
    {
    System.out.println("SEARH");

    ShopDAO sDao = JdbcDaoFactory.getInstance().getShopJdbcDAO();
        if (sDao == null) return;

        if(lookupDbColumnName.get(sortColumnName) == null) return;


        List<WebShopBean> newWebshopList;

        newWebshopList = new ArrayList<WebShopBean>();

        // new search! Should recieve actionEvent
        if(e != null)
            oldSearchString = searchString;
        
        // AcionEvent == null, we do just an update
            // due to ascending
            Collection<Shop> shops = sDao.searchForShopsByParameters(oldSearchString,
                lookupDbColumnName.get(sortColumnName), ascending);
                if(shops!=null)
                    for(Shop shop: shops)

                        newWebshopList.add(new WebShopBean(shop));
        
                    this.webshopList = newWebshopList;
                    oldAscending = ascending;
                    oldSortColumnName = sortColumnName;

    }

    public void showAll(ActionEvent e)
    {
        this.oldSearchString = "";
        search(null);
    }
/*
 *  AutoCompletion
 *
 *
 */
    public void selectInputValueChanged(ValueChangeEvent event)
    {
        if (event.getComponent() instanceof SelectInputText)
        {
            SelectInputText autoComplete = (SelectInputText) event.getComponent();
            String newWord = (String) event.getNewValue();
            matchPossibilities = generateMatches(newWord, 10);

            if (autoComplete.getSelectedItem() != null)
                searchString = (String) autoComplete.getSelectedItem().getValue();
        }
    }
    public List<SelectItem> generateMatches(String searchWord, int maxMatches)
    {
        ArrayList<String> matchList = new ArrayList();
        List<SelectItem> returnCollection = new ArrayList<SelectItem>();

        if ((searchWord == null) || (searchWord.trim().length() == 0))
            return returnCollection;

        ShopDAOCore shopDAO = ShopJdbcDAOCore.getInstance();

        for(String s : shopDAO.searchForStringShops(searchWord, maxMatches))
            matchList.add(s);
        Collections.sort(matchList);

        if(matchList.size() < maxMatches)
            maxMatches = matchList.size();

        for(String s : matchList.subList(0, maxMatches))
            returnCollection.add(new SelectItem(s));
        return returnCollection;
    }


    public WebShopBean getAddShop()
    {
        return addShop;
    }

    public void setAddSjop(WebShopBean addShop)
    {
        this.addShop = addShop;
    }

}
